$(function () {
  var App = {

    //TODO: Find a better way to set these from config.php
    baseUrl : '/zwop/',
    maxCharacters: 320,

    init: function () {
      this.setElements();
      this.bindEvents();
    },

    // Cache all the jQuery selectors for easy reference.
    setElements: function () {
      //config operation
      this.$systemRestartButton = $('#btnSystemRestart');
      
    },

    // Bind document events and assign event handlers.
    bindEvents: function () {
      this.$systemRestartButton.on('click',this.systemRestart);
    },


    /* *************************************
     *             Event Handlers
     * ************************************* */

    
    /**
     * Click handler for the Restart button in zwop_system.php. 
     * It restarts the specified system_name or system_ip and displays
     * restart result on `pre`
     * @param e event
     */
    systemRestart : function (e) {
        var systemName = $('#systemSelect').val();
        var systemFlag= $.trim(document.getElementById("restartFlag").textContent);
        
        var selectedData = {
            restart_object : systemName,
            restart_flag : systemFlag

        };
        var tmpData={};
        var postUrl = App.baseUrl + 'zwop_system/exec_restart';
        $.ajax({
            type: 'POST',
            url: postUrl,
            dataType: 'text',
            data: selectedData,
            beforeSend: App.beforeSendFunc,
            success: App.systemRestartSuccess,
            error: App.alertError
        })
    },

    /* *************************************
     *             AJAX Callbacks
     * ************************************* */
    
    /**
     * define a pre-process function before send ajax request
     */
    beforeSendFunc: function(){
                    $('#load').append('Running Command, please wait a moment');
                    },
    
    /**
     * A selected config file has been opened, and the server has responed (or errored)
     * @param response
     */
    systemRestartSuccess : function(response) {
                         if (response){
                             document.getElementById('load').innerHTML = (response);
                         }
                     },
    /**
     * Util method for blasting an error message on the screen.
     * @param error
     */
    alertError : function( error ) {
       var args = arguments;
       var msg = error.responseText;
    }

  };

  App.init();

});
